<template>
	<view class="container">
		<view class="head"></view>
		<view class="top">
			<image :src="shopDetail.logo" mode=""></image>
			<view class="text">
				<view class="name">
					{{data.cShopShowName}}
				</view>
				<view class="btn">{{data.iType==1?'自营':'非自营'}}</view>
				<view class="but" @click="toFavorite">
					{{isSelected?'取消关注':'关注'}}
				</view>
				<view class="p">
					<!-- 电话：{{data.cTelephone}} -->
					{{count}}人关注
				</view>
			</view>
		</view>
		<view class="bb">
			<view class="li">
				店铺主体 <text>{{data.cShopName}}</text>
			</view>
			<view class="li">
				入驻时间 <text>{{data.pubuts}}</text>
			</view>
			<view class="ul">
				<view class="names">
					店铺标签
				</view>
				<view class="text">
					{{data.cShopTag}}
				</view>
			</view>
		</view>
		<view class="bb">
			<view class="li lis">
				商务合作
			</view>
		</view>
	</view>
</template>

<script> 
	export default {
		data() {
			return {
				data:{},
				isSelected:false,
				collection:[],
				currentId:'',
				id:'',
				count:0,
				shopDetail:{}
			};
		},

		async onLoad(options) {
			let that = this
			this.id = options.id
			this.$Request.post4(
				this.$api.httpApi.home.shopdetail, {
					"shop_id": options.id
				}
			).then(res => {
				if (res.code == 1) {
					console.log('res.data', res)
					this.shopDetail = res.data
			
				}
			})
			if (that.$SysCache.get('token')) {
				that.$Request.post(
					that.$api.httpApi.goods.getShopFavorites + '&token=' + that.$SysCache.get('token'),
				).then(res => {
					if (res.code == 200) {
						that.collection = res.data.pager.data;
						if(that.collection.length>0){
							// this.isSelected = true
							res.data.pager.data.forEach(item=>{
								if(item.shopid == options.id){
									this.currentId = item.id
									this.isSelected = true
								}
							})
						}else{
							this.isSelected = false
						}
						
					}
				});
			}
			this.$Request.post(
				this.$api.httpApi.brand.getShopById, {
					"id": options.id
				}
			).then(res => {
				if(res.code == 200){
					this.data=res.data
				}
			})
			
			this.getCount()
			
		
		},

		methods: {
			
			getCount(){
				this.$Request.post(
					this.$api.httpApi.goods.getFavoriteCount, {
						"id": this.id
					}
				).then(res => {
					if(res.code == 200){
						this.count=res.data
					}
				})
			},
			//收藏
			toFavorite() {
				
				let datas = {
					"ids": [this.data.id],
					"token": this.$SysCache.get('token') || null,
				}
				let delData = {
					"ids":[this.currentId],
					"token": this.$SysCache.get('token') || null,
				}
				let that = this;
				if (this.isSelected) {
					this.$Request.post(this.$api.httpApi.goods.delFavorites + "&token=" + this.$SysCache.get('token'),
						delData).then(
						res => {
							if (res.code == 200) {
								this.isSelected = false
								this.$api.prePage().getFav(this.isSelected);
								//接收传值,id里面放的是标题，因为测试数据并没写id 
			
								if (that.$SysCache.get('token')) {
									that.$Request.post(
										that.$api.httpApi.goods.getShopFavorites + '&token=' + that.$SysCache.get('token'),
									).then(res => {
										if (res.code == 200) {
											that.collection = res.data.pager.data;
											if(that.collection.length>0){
												// this.isSelected = true
												res.data.pager.data.forEach(item=>{
													if(item.shopid == this.id){
														that.currentId = item.id
														this.isSelected = true
														this.$api.prePage().getFav(this.isSelected);
													}
												})
											}else{
												this.isSelected = false
												this.$api.prePage().getFav(this.isSelected);
											}
										}
									});
								}
								uni.showToast({
									title: res.message,
									duration: 2000
								})
							}
						})
			
				} else {
					this.$Request.post(this.$api.httpApi.goods.addShopFavorit + "&token=" + this.$SysCache.get('token'), datas).then(
						res => {
							if (res.code == 200) {
			
			
								//接收传值,id里面放的是标题，因为测试数据并没写id 
			
								if (that.$SysCache.get('token')) {
									that.$Request.post(
										that.$api.httpApi.goods.getShopFavorites + '&token=' + that.$SysCache.get('token'),
									).then(resq => {
										if (resq.code == 200) {
											that.collection = resq.data.pager.data;
											resq.data.pager.data.forEach(item=>{
												if(item.shopid == this.id){
													that.currentId = item.id
													this.isSelected = true
													this.$api.prePage().getFav(this.isSelected);
												}
											})
										}
									});
								}
								uni.showToast({
									title: res.message,
									duration: 2000
								})
							}else{
								this.isSelected = false
								this.$api.prePage().getFav(this.isSelected);
							}
						})
				}
			
				this.getCount()
			
				// this.favorite = !this.favorite;
			},
		},
		

	}
</script>

<style lang="less" scoped>
	page{
		height: 100%;
	}
	.container{
		height: 100%;
		background-color: #EBEBEB;
		position: relative;
		z-index: 10;
		.head{
			height: calc(242rpx - 84rpx);
			background-color: #141414;
			width: 100%;
			border-bottom-left-radius: 30rpx;
			position: absolute;
			z-index: 4;
			top: 0;
			left: 0;
		}
		.bb{
			margin: auto;
			margin-top: 8rpx;
			background-color: #fff;
			width: 670rpx;
			
			.ul{
				padding-left: 34rpx;
				padding-right: 34rpx;
				padding-top: 30rpx;
				padding-bottom: 30rpx;
				font-size: 24rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #333333;
				.names{
					display: inline-block;
					vertical-align: middle;
					margin-right: 26rpx;
					height: 42rpx;
					line-height: 42rpx;
				}
				.text{
					display: inline-block;
					vertical-align: middle;
					height: 42rpx;
					background: rgba(0,0,0,0.15);
					border-radius: 6rpx;
					margin-right: 12rpx;
					margin-top: 14rpx;
					line-height: 42rpx;
					padding-left: 30rpx;
					padding-right: 30rpx;
					font-size: 20rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #747474;
					
						
				}
			}
			.li{	
				height: 93rpx;
				border-bottom: 1px solid #ebebeb;
				line-height: 93rpx;
				font-size: 24rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				padding-left: 34rpx;
				padding-right: 34rpx;
				color: #333333;
				text{
					margin-left: 26rpx;
					font-size: 24rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #888888;
				}
			}
			.lis{
				text-align: center;
			}
		}
		.top{
			width: 670rpx;
			height: 242rpx;
			background: #FFFFFF;
			margin: auto;
			position: relative;
			z-index: 10;
			padding: 35rpx;
			image{
				display: inline-block;
				vertical-align: middle;
				width: 167rpx;
				height: 167rpx;
				margin-right: 39rpx;
			}
			.text{
				display: inline-block;
				vertical-align: middle;
				width: calc(100% - 167rpx - 39rpx);
				position: relative;
				.but{
					position: absolute;
					width: 122rpx;
					height: 43rpx;
					background: #F0406F;
					box-shadow: 3rpx 3rpx 6rpx 0rpx rgba(240, 64, 111, 0.27);
					border-radius: 22rpx;
					text-align: center;
					line-height: 43rpx;
					font-size: 18rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #FFFFFF;
					right: 43rpx;
					top: 26rpx;
				}
				.name{
					font-size: 28rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #000000;
				}
				.tab{
					margin-top: 11rpx;
					width: 61rpx;
					height: 34rpx;
					background: #000000;
					border-radius: 5rpx;
					text-align: center;
					line-height: 34rpx;
					font-size: 18rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #FFFFFF;
				}
				.p{
					margin-top: 29rpx;
					font-size: 22rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #666666;
				}
				.btn{
					padding: 6upx 10upx;
					display: flex;
					justify-content: center;
					align-items: center;
					color: #fff;
					background-color: #000;
					font-size: 18upx;
					width: 70upx;
					border-radius: 5upx;
					margin-top: 5upx;
					}
			}
		}
	}



</style>
